﻿using FastJSON;
using FineAdmin.Common;
using FineAdmin.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FineAdmin.Service
{
    /// <summary>
    /// 
    /// </summary>
    public static class HuiJIRedisService
    {
        private static TimeSpan sevenDays = TimeSpan.FromDays(7);

        private static TimeSpan tempTimeSpan = TimeSpan.FromMinutes(30);

        private static TimeSpan LikeOrTrampleTimeSpan= TimeSpan.FromSeconds(66);

        public static RedisHelper MaterialGoodshelper;

        public static RedisHelper LikeOrTrampleHelper;
        static HuiJIRedisService()
        {
            MaterialGoodshelper = new RedisHelper(1);
            LikeOrTrampleHelper=new RedisHelper(2);
        }

        public static string MaterialGoodsKey(MaterialGoodsModel model)
        {
            return "item:" + "M" + model.Mid + ":ItemID" + model.ItemId;
        }
        public static string MaterialGoodsKey(string mid, string itemId)
        {
            return "item:" + "M" + mid + ":ItemID" + itemId;
        }
        /// <summary>
        /// 判断MaterialGoods中的数据是否在缓存中
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool MaterialGoodsExists(MaterialGoodsModel model)
        {
            return MaterialGoodshelper.KeyExists(MaterialGoodsKey(model));
        }
        public static bool MaterialGoodsExists(string mid, string itemId)
        {
            return MaterialGoodshelper.KeyExists(MaterialGoodsKey(mid, itemId));
        }
        /// <summary>
        /// 设置MaterialGoods中的数据到缓存中 有效期7天
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool MaterialGoodsSet(MaterialGoodsModel model)
        {
            if (model.Mid == -1)
            {
                return MaterialGoodshelper.StringSet(MaterialGoodsKey(model), JSON.ToJSON(model), tempTimeSpan);
            }
            return MaterialGoodshelper.StringSet(MaterialGoodsKey(model), JSON.ToJSON(model), sevenDays);
        }
        /// <summary>
        /// 获取MaterialGoods中的数据
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="itemId"></param>
        /// <returns></returns>
        public static string MaterialGoodsGet(string mid, string itemId)
        {
            return MaterialGoodshelper.StringGet(MaterialGoodsKey(mid, itemId));
        }

        /// <summary>
        /// 设置缓存 1 喜欢 0 踩 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool LikeOrTrampleSet(string sessionid, int type)
        {
            var prefix = type == 1 ? "Like:" : "Trample:";

            return LikeOrTrampleHelper.StringSet(prefix + sessionid, sessionid, LikeOrTrampleTimeSpan);
        }
        /// <summary>
        /// 读取缓存 1 喜欢 0 踩 
        /// </summary>
        /// <param name="sessionid"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool LikeOrTrampleExists(string sessionid, int type)
        {
            var prefix = type == 1 ? "Like:" : "Trample:";

            return LikeOrTrampleHelper.KeyExists(prefix+sessionid);
        }
    }


}
